package com.kunbo.cn.app.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.aliyuncs.exceptions.ClientException;
import com.kunbo.cn.app.service.AppUserService;
import com.kunbo.cn.app.util.Verificationcode;
import com.kunbo.cn.util.encrypt.MD5Utils;

@Service
public class AppUserImpl implements AppUserService{

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	
	@Override
	public Map<String,Object> login(String userName, String password,String userType) {
		Map<String,Object>  map = new HashMap<String,Object>();
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		String sql = "";
		if(userType.equals("transport_unit_management") && !"".equals(userName) && !"".equals(password)) {
			sql += "select * from transport_unit_management where login_id = '" + userName + "' and login_password = '" + MD5Utils.md5(password)
					+ "'";
			list = jdbcTemplate.queryForList(sql);
			if(list.size() > 0 ) {
				list.get(0).put("loginType", userType);
				map.put("user", list);
				map.put("unit", list.get(0));
				map.put("stateCode", "success");
				return map;
			}else {
				map.put("stateCode", "用户名或密码错误");
				return null;
			}
		}else if(userType.equals("business_customer_users_manage") && !"".equals(userName) && !"".equals(password)){
			
			//sql += "select * from " + userType + ",select  where login_name = '" + userName + "' and login_password = '" + MD5Utils.md5(password)+ "'";
			sql += "select * from business_customer_users_manage  "
					+ "where login_name = '"+userName+"' and login_password = '"+MD5Utils.md5(password)+"'";
			list = jdbcTemplate.queryForList(sql);
			if(list.size() > 0 ) {
				list.get(0).put("loginType", userType);
				String customer = "select * from business_customer_manage where id = " + Integer.parseInt(list.get(0).get("customer").toString());
				map.put("user", list);
				map.put("unit", jdbcTemplate.queryForMap(customer));
				map.put("stateCode", "success");
				return map;
			}else {
				map.put("stateCode", "用户名或密码错误");
				return null;
			}
		}else if(userType.equals("business_supplier_users_manage") && !"".equals(userName) && !"".equals(password)){
		sql += "select * from " + userType + " where login_name = '" + userName + "' and login_password = '" + MD5Utils.md5(password)+ "'";
		list = jdbcTemplate.queryForList(sql);
		if(list.size() > 0 ) {
			list.get(0).put("loginType", userType);
			String supplier = "select * from business_supplier_manage where id = " + Integer.parseInt(list.get(0).get("supplier").toString());
			map.put("user", list);
			map.put("unit", jdbcTemplate.queryForMap(supplier));
			map.put("stateCode", "success");
			return map;
		}else {
			map.put("stateCode", "用户名或密码错误");
			return null;
		}
		
		}
		map.put("stateCode", "用户名或者密码为空");
		return map;
	}
	@Override
	public String validatePhone(String phone) {
		// TODO Auto-generated method stub
		try {
			return Verificationcode.sendMsg(phone);
		} catch (ClientException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Boolean setNewPassword(String phone, String password,String userType) {
		// TODO Auto-generated method stub
		String sql = "";
		if(userType.equals("transport_unit_management")) {
			sql += "update transport_unit_management set login_password = '" + MD5Utils.md5(password) + "' where contact_number = '" + phone + "'";
		}else {
			sql += "update " + userType + " set login_password = '" + MD5Utils.md5(password) + "' where phone = '" + phone + "'";
		}
		int i = jdbcTemplate.update(sql);
		if(i > 0 )
			return true;
		else
		    return false;
	}

	@Override
	public Boolean setPassword(String oldPassword, String newPassword,String userType) {
		// TODO Auto-generated method stub
		String sql = "update " + userType + " set login_password = '" + MD5Utils.md5(newPassword) + "' where login_password = '" +  MD5Utils.md5(oldPassword) + "'"; 
		int i = jdbcTemplate.update(sql);
		if(i > 0 )
			return true;
		else
		    return false;
	}

	@Override
	public Boolean setNewPhone(String oldPhone, String newPhone,String userType) {
		// TODO Auto-generated method stub
		String sql = "";
		if(userType.equals("transport_unit_management")) {
		sql += "update transport_unit_management set contact_number = '" + newPhone + "' where contact_number = '" + oldPhone + "'"; 
		}else {
			sql += "update " + userType + " set phone = '" + newPhone + "' where phone = '" + oldPhone + "'";
		}
		int i = jdbcTemplate.update(sql);
		if(i > 0 )
			return true;
		else
		    return false;
	}
	
	public static void main(String[] args) {
		 System.out.println(MD5Utils.md5("999999"));
	}

}
